package com.sqsf.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

//读取properties配置文件
@PropertySource("classpath:jdbc.properties")//等价于<context:property-placeholder location="classpath:jdbc.properties"/>
public class DruidConfig {
    //与配置文件不同 需要声明变量保存读取数据
    //使用@Value("${name}")的形式注入数据
    @Value("${jdbc.driver}")
    private  String driver;
    @Value("${jdbc.url}")
    private  String url;
    @Value("${jdbc.username}")
    private  String username;
    @Value("${jdbc.password}")
    private  String password;

    //配置当前方法返回的对象给spring容器管理
    //<bean id="druid" factory-bean="factory" factory-method="druid"/>
    @Bean("druid")
    public DruidDataSource druid(){
        DruidDataSource druid=new DruidDataSource();
        druid.setDriverClassName(driver);
        druid.setUrl(url);
        druid.setUsername(username);
        druid.setPassword(password);
        return druid;
    }
}
